package com.stbu.controller;

import com.stbu.controller.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

@Controller
@SessionAttributes(names = {"regDate"},types = {User.class})
@RequestMapping("/user")
public class UserController {
    @GetMapping("/toreg")
    public String toReg(String userName, String password) {
        return "reg";
    }
    @GetMapping("/reg")
    public String reg(@RequestParam("username") String u1,
                      @RequestParam("pwd")String password, Model model){
        User user= new User();
        user.setUsername(u1);
        user.setUsername(password);
        Date regDate=new Date();
        model.addAttribute("regDate",regDate);
        model.addAttribute("user",user);
        return "forward:tologin";
    }
    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }
    public ModelAndView Login(@RequestParam("username")String ulogin,
                              @RequestParam("pwd")String pLogin,
                              @SessionAttribute("user")User regUser,ModelAndView mView){
        String msg="";
        if(ulogin.equalsIgnoreCase(regUser.getUsername())&&pLogin.equalsIgnoreCase(regUser.getPassword())){
            msg="登录成功";
        }else{
            msg="登录失败";
        }
        mView.addObject("msg",msg);
        mView.addObject("username",ulogin);
        mView.setViewName("result");
        return mView;
    }
}
